package weaver.interfaces.zhlx.duss.action.financemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 业务收款认领流程归档后调用接口流水认领结果到SAP系统
 * @ModifyLog:
 **/
public class WeaYwskrllcSapAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaYwskrllcSapAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            String billname = "uf_zjjhhzb";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String jbrwb = Util.null2String(rs.getString("jbrwb"));
            String gsdm = Util.null2String(rs.getString("gsdm"));
            String khbm = Util.null2String(rs.getString("khbm"));
            String sappzh = Util.null2String(rs.getString("sappzh"));
            if ("".equals(sappzh)) {
                JSONObject dataJson = new JSONObject();
                JSONObject headerJson = new JSONObject();
                headerJson.put("ZSHOUKUANLEIXING", "1");
                headerJson.put("ZLIUCHENGID", lcbh);
                headerJson.put("USNAM", jbrwb);
                headerJson.put("BUKRS", gsdm);
                headerJson.put("KUNNR1", khbm);
                String skbj = "0";
                int count = 0;
                String bz = "";
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    if (count == 0) {
                        bz = Util.null2String(rs.getString("bz"));
                    }
                    count++;
                }
                headerJson.put("WAERS", bz);
                JSONArray yhlsList = new JSONArray();
                JSONArray zjjhList = new JSONArray();
                JSONArray ysxxList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject yhlsJson = new JSONObject();
                    yhlsJson.put("SERIAL_NO", Util.null2String(rs.getString("yxjylsh")));
                    yhlsJson.put("ZWELS", Util.null2String(rs.getString("fkfs")));
                    yhlsJson.put("BANKN", Util.null2String(rs.getString("skyxzh")));
                    yhlsJson.put("ZFANGXIANG", skbj);
                    yhlsJson.put("WRBTR2", Util.null2String(rs.getString("sxf")));
                    yhlsJson.put("BUDAT1", Util.null2String(rs.getString("jyrq")).replace("-", ""));
                    yhlsJson.put("WRBTR1", Util.null2String(rs.getString("jyje")));
                    yhlsJson.put("RSTGR", Util.null2String(rs.getString("yym1")));
                    yhlsJson.put("ZUONR", Util.null2String(rs.getString("pjbh")));
                    yhlsList.add(yhlsJson);
                }
                headerJson.put("YHLS", yhlsList);
                rs.executeQuery("select * from " + tablename + "_dt2 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject zjjhJson = new JSONObject();
                    zjjhJson.put("ZJIHUAID", Util.null2String(rs.getString("zjjhid")));
                    zjjhJson.put("GJAHR", Util.null2String(rs.getString("yspzhjnd")));
                    zjjhJson.put("BELNR", Util.null2String(rs.getString("lzdjhwb")));
                    zjjhJson.put("BUZEI", Util.null2String(rs.getString("yspzxxmbh")));
                    zjjhJson.put("HKONT", Util.null2String(rs.getString("wlkmbm")));
                    zjjhJson.put("WRBTR5", Util.null2String(rs.getString("ysje")));
                    zjjhJson.put("WRBTR3", Util.null2String(rs.getString("rlje")));
                    zjjhList.add(zjjhJson);
                }
                headerJson.put("ZJJH", zjjhList);
                rs.executeQuery("select max(jyrq) from " + tablename + "_dt1 where mainid=?", mainid);
                rs.next();
                String jyrq = Util.null2String(rs.getString(1)).replace("-", "");
                rs.executeQuery("select * from " + tablename + "_dt3 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject ysxxJson = new JSONObject();
                    ysxxJson.put("VBELN", Util.null2String(rs.getString("xsddbh")));
                    ysxxJson.put("HKONT", Util.null2String(rs.getString("kmdm")));
                    ysxxJson.put("WRBTR4", Util.null2String(rs.getString("rlje")));
                    ysxxJson.put("ZFBDT", jyrq);
                    ysxxList.add(ysxxJson);
                }
                headerJson.put("YSXX", ysxxList);
                dataJson.put("HEADER", headerJson);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaYwskrllcSapAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaYwskrllcSapAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequest("FI012_2", dataJson, dataMap);
                writeLog(">>>>>WeaYwskrllcSapAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    String dataStr = outputJson.getString("DATA");
                    JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                    JSONObject returnJson = JSONObject.parseObject(returnDataJson.getString("RETURN"));
                    if ("S".equalsIgnoreCase(msgType)) {
                        String belnr = returnJson.getString("MSG_BELNR");
                        String bukrs = returnJson.getString("MSG_BUKRS");
                        String gjahr = returnJson.getString("MSG_GJAHR");
                        JSONArray returnZjjhList = JSONArray.parseArray(returnJson.getString("ZJJH"));
                        String sql = "update " + tablename + " set sappzh=?,sapgsdm=?,saphjnd=? where requestid=?";
                        rs.executeUpdate(sql, belnr, bukrs, gjahr, requestid);
                        for (int i = 0; i < returnZjjhList.size(); i++) {
                            String zjihuaid = returnZjjhList.getJSONObject(i).getString("ZJIHUAID");
                            String buzei = returnZjjhList.getJSONObject(i).getString("BUZEI");
                            String dmbtr = returnZjjhList.getJSONObject(i).getString("DMBTR");
                            String sql1 = "update " + tablename + "_dt2 set syjexxmbh=?,syje=? where zjjhid=?";
                            rs.executeUpdate(sql1, buzei, dmbtr, zjihuaid);
                            rs.executeQuery("select * from uf_zjjhhzb where jhid=?", zjihuaid);
                            if (rs.next()) {
                                String[] columnName = SapUtil.getBillFieldId(billname).split(",");
                                Map<String, Object> mainMap = new HashMap<>();
                                for (String column : columnName) {
                                    String columnValue = Util.null2String(rs.getString(column));
                                    mainMap.put(column.toLowerCase(), columnValue);
                                }
                                String zsbbje = SapUtil.getbbje(Util.null2String(mainMap.get("bz")), dmbtr);
                                mainMap.put("zsbbje", zsbbje);
                                mainMap.put("byjhsfkbb", zsbbje);
                                mainMap.put("cwfhjebb", zsbbje);
                                mainMap.put("lzdjh", belnr);
                                mainMap.put("ydzqysfyeyb", dmbtr);
                                mainMap.put("byjhsfkyb", dmbtr);
                                mainMap.put("cwfhjeyb", dmbtr);
                                mainMap.put("yfpzhjnd", gjahr);
                                SapUtil.insertModelData("uf_zjjhhzb", 1, mainMap);
                            }
                        }
                    } else {
                        String mesg = returnJson.getString("MESG");
                        JSONArray mesgList = JSONArray.parseArray(mesg);
                        for (int j = 0; j < mesgList.size(); j++) {
                            String msgType1 = mesgList.getJSONObject(j).getString("MSG_TYPE");
                            if ("E".equalsIgnoreCase(msgType1)) {
                                message += mesgList.getJSONObject(j).getString("MSG_TEXT") + ";";
                            }
                        }
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaYwskrllcSapAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }


}